/** 
 * @copyright		all rights reserved.
 * @author			how_fly@yahoo.com
 * @brief			user can initialize all the schemas
 * 
 * @version			1.0
 * @date
 * 
 * 
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#include "init_all_db.h"

/**
 *  @brief initialize all the tables in one schema
 *  @param [in]MYSQL: the connection to mysql
 *  @param [in]const char* table: the name of table will be deleted. 
 *  @return int: 0 is good, -1 is error.
 */ 
int create_db(MYSQL& mysql,const char* table)
{
	char sql[2048] = {0};
	for(int i = 0; i <512; i++) ///there are 512 table in each schema
	{
		sprintf(sql, table, i);
		if( mysql_real_query(&mysql, sql, strlen(sql)) )
		{
			printf("error %s %d:%s\n",table,mysql_errno(&mysql),mysql_error(&mysql));
			mysql_close(&mysql);
			return -1;
		}
	}
	return 0;
}

/**
 *  @brief initialize all the tables in one schema
 *  @param [in]MYSQL: the connection to mysql
 *  @param [in]conf_schema:the configure of schema
 *  @param [in]const char* table: the name of table will be initialized. 
 *  @return int: 0 is good, -1 is error.
 */ 
int create_schema_table(MYSQL& mysql,const conf_schema& _conf_schema, const char* table)
{
	mysql_init(&mysql);

	if(!mysql_real_connect(&mysql, 
		_conf_schema.host.c_str(),
		_conf_schema.user.c_str(),
		_conf_schema.password.c_str(),
		_conf_schema.name.c_str(),
		 0, NULL, 0))
	{
		printf("error, connect \n");
		return -1;
	}
	if(create_db(mysql, table) != 0)
	{
		mysql_close(&mysql);
		return -1;
	}
	printf("create %s is ok\n",table);
	mysql_close(&mysql);
	return 0;
}

int init_all_db(const configure& conf)
{
	MYSQL mysql;
	
	///initialize keyword schema
	char keyword_table[2048] = 
		"CREATE TABLE KEYWORD_%03d (	\
  			ID int(10) unsigned NOT NULL auto_increment,	\
  			KEYWORD varchar(255) NOT NULL,\
  			WEIGHT int(8) unsigned  , \
  			NEXT int(8) unsigned NOT NULL default '1',\
  			COUNT_0 int(8) unsigned ,\
  			URL_TB_0 varchar(24),\
  			URL_MD5_0 varchar(33),\
  			URL_0 varchar(255), \
  			CHECKDATE_0 date,\
  			COUNT_1 int(8) unsigned ,\
  			URL_TB_1 varchar(24),\
  			URL_MD5_1 varchar(33),\
  			URL_1 varchar(255),\
  			CHECKDATE_1 date,\
  			COUNT_2 int(8) unsigned ,\
  			URL_TB_2 varchar(24),\
  			URL_MD5_2 varchar(33),\
  			URL_2 varchar(255),\
  			CHECKDATE_2 date,\
  			COUNT_3 int(8) unsigned ,\
  			URL_TB_3 varchar(24),\
  			URL_MD5_3 varchar(33),\
  			URL_3 varchar(255),\
  			CHECKDATE_3 date,\
  			COUNT_4 int(8) unsigned ,\
  			URL_TB_4 varchar(24),\
  			URL_MD5_4 varchar(33),\
  			URL_4 varchar(255),\
  			CHECKDATE_4 date,\
  			COUNT_5 int(8) unsigned ,\
  			URL_TB_5 varchar(24),\
  			URL_MD5_5 varchar(33),\
  			URL_5 varchar(255),\
  			CHECKDATE_5 date,\
  			COUNT_6 int(8) unsigned ,\
  			URL_TB_6 varchar(24),\
  			URL_MD5_6 varchar(33),\
  			URL_6 varchar(255),\
  			CHECKDATE_6 date,\
  			COUNT_7 int(8) unsigned ,\
  			URL_TB_7 varchar(24),\
  			URL_MD5_7 varchar(33),\
  			URL_7 varchar(255),\
  			CHECKDATE_7 date,\
  			COUNT_8 int(8) unsigned ,\
  			URL_TB_8 varchar(24),\
  			URL_8 varchar(255),\
  			URL_MD5_8 varchar(33),\
  			CHECKDATE_8 date,\
  			COUNT_9 int(8) unsigned ,\
  			URL_TB_9 varchar(24),\
  			URL_9 varchar(255),\
  			URL_MD5_9 varchar(33),\
  			CHECKDATE_9 date,\
  			PRIMARY KEY  (ID)	\
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED";
	create_schema_table(mysql, conf.keyword, keyword_table);
	
	///initialize url schema
	char url_table[1024] =
		"CREATE TABLE URL_%03d (	\
  			MD5 varchar(33) NOT NULL,	\
  			URL varchar(255) NOT NULL, \
  			CONTENT varchar(512) NOT NULL,\
  			CHECKDATE date NOT NULL, \
  			PRIMARY KEY  (MD5)	\
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED";
	create_schema_table(mysql, conf.url, url_table);
	
	///initialize pending schema
	char pending_table[] = 
		"CREATE TABLE PENDING_%03d ( \
			MD5 varchar(33) NOT NULL,	\
  			URL varchar(255) NOT NULL, \
  			CHECKDATE date NOT NULL, \
  			PRIMARY KEY  (MD5)	\
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED";
	create_schema_table(mysql, conf.pending, pending_table);	
	
	///initialize invalid schema
	char invalid_table[] = 
		"CREATE TABLE INVALID_%03d ( \
			MD5 varchar(33) NOT NULL,	\
  			URL varchar(255) NOT NULL, \
  			CHECKDATE date NOT NULL, \
  			PRIMARY KEY  (MD5)	\
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED";
	create_schema_table(mysql, conf.invalid, invalid_table);

	
	///initialize dict schema
	char dict_table[] = 
		"CREATE TABLE DICT_%03d ( \
			WORD varchar(255) NOT NULL, \
			COUNT int(16) unsigned ,\
  			PRIMARY KEY  (WORD)	\
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED";
	create_schema_table(mysql, conf.dict, dict_table);
	
	return 0;
}

